<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>利用别名实现一个用户一个索引 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/faking-it.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/faking-it.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/faking-it.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/faking-it.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="scale.html">扩容设计</a></span>
»
<span class="breadcrumb-node">利用别名实现一个用户一个索引</span>
</div>
<div class="navheader">
<span class="prev">
<a href="shared-index.html">« 共享索引</a>
</span>
<span class="next">
<a href="one-big-user.html">一个大的用户 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="faking-it"></a>利用别名实现一个用户一个索引 (Faking Index per User with Aliases)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/410_Scaling/70_Faking_it.asciidoc">edit</a>
</h2>
</div></div></div>
<p>为了保持设计的简洁，我们想让我们的应用认为我们为每个用户都有一个专门的索引 —— 或者例子中的每个论坛一个 —— 尽管实际上我们用的是一个大的<a class="xref" href="shared-index.html" title="共享索引">共享索引(shared index)</a>。
因此，我们需要一种方式将 <code class="literal">routing</code> 值及过滤器隐含于 <code class="literal">forum_id</code> 中。</p>

<p>索引别名允许我们这样做。当你将一个别名与一个索引关联时，可以指定一个过滤器和一个路由值：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /forums/_alias/baking
{
  "routing": "baking",
  "filter": {
    "term": {
      "forum_id": "baking"
    }
  }
}</pre>
</div>
<p>现在我们可以将别名<code class="word">baking</code>视为一个单独的索引。索引至别名<code class="word">baking</code>的文档会自动地应用我们自定义的路由值：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /baking/post/1 <a id="CO296-1"></a><i class="conum" data-value="1"></i>
{
  "forum_id": "baking", <a id="CO296-2"></a><i class="conum" data-value="1"></i>
  "title":    "Easy recipe for ginger nuts",
  ...
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO296-1"><i class="conum" data-value="1"></i></a><a href="#CO296-2"></a></p>
</td>
<td align="left" valign="top">
<p>我们还是需要为过滤器指定 <code class="word">forumn_id</code> 字段，但自定义路由值已经是隐含的了。</p>
</td>
</tr>
</table>
</div>
<p>对别名<code class="word">baking</code>的查询只会在自定义路由关联的分片上运行，并且结果也自动按照我们指定的过滤器进行了过滤：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /baking/post/_search
{
  "query": {
    "match": {
      "title": "ginger nuts"
    }
  }
}</pre>
</div>
<p>当对多个论坛进行搜索时可以指定多个别名：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /baking,recipes/post/_search <a id="CO297-1"></a><i class="conum" data-value="1"></i>
{
  "query": {
    "match": {
      "title": "ginger nuts"
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO297-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>两个 <code class="literal">routing</code> 的值都会应用，结果会匹配任意一个过滤器。</p>
</td>
</tr>
</table>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="shared-index.html">« 共享索引</a>
</span>
<span class="next">
<a href="one-big-user.html">一个大的用户 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>